import re
log = 'Sep 26 2021 23:11:02-08:00 Layer3Switch-1 L2IFPPI/4/MFLPVLANALARM:OID 1.3.6.1.4.1.2011.5.25.160.3.7 MAC move detected, VlanId = 54, MacAddress = 0000-5e00-0136, Original-Port = GE0/0/1, Flapping port = GE0/0/2. Please check the network accessed to flapping port.'
#VlanId = 54, MacAddress = 0000-5e00-0136, Original-Port = GE0/0/1, Flapping port = GE0/0/2.
#template=r'.* VlanId = (\d+), MacAddress = (\S+), Original-Port = (\S+), Flapping port = (\S+)'
template=r'.* VlanId = (\d+), MacAddress = (\S+), Original-Port = (\S+), Flapping port = (.*)\.\s'
#template=r'.* VlanId = (\d+), MacAddress = (\S+), Original-Port = (\S+), Flapping port = (\GE0\Wd+\Wd+)'
#最后一项用（）所需要的信息取了出来，其它的就通过写描述的形式省略掉了

#\s表示匹配任意空白字符
match=re.search(template,log)
print(match)
print(match.group())
print(match.group(0))
print(match.group(1))
print(match.group(2))
print(match.group(3))
print(match.group(4))

#练习groups的写法
print(match.groups())

t1=r' .* VlanId = (?P<vlan>\d+), MacAddress = (?P<mac>\S+), Original-Port = (?P<originalport>\S+), Flapping port =(?P<flappingport>.*)\.\s'
match=re.search(t1,log)
print(match)
print(match.groupdict())

#练习start end方法
#这两个方法返加匹配在原字符串的索引位置

print(match.start())

print(match.end())
print(match)

print(len(log))

print(match.span())